package com.jy.api.controller.admin;

import com.jy.api.dao.StoreProfileDao;
import com.jy.api.domain.StoreProfile;
import com.jy.api.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * Created by hanxi on 2017/10/23.
 */
@RestController
public class StoreProfileController {
    private StoreProfileDao dao;

    @GetMapping(path = "/v1/admin/profile")
    public ResponseEntity<StoreProfile> get(@RequestParam String storeId) {
        StoreProfile profile = dao.findByStoreId(storeId);
        if (null == profile) {
            return ResponseEntity.notFound().build();
        } else {
            return ResponseEntity.ok(profile);
        }
    }

    @PutMapping(path = "/v1/admin/profile")
    public void submit(@RequestBody StoreProfile profile) {
        StoreProfile stale = dao.findByStoreId(profile.getStoreId());
        if (null == stale) {
            profile.setId(UuidUtil.getUuid());
        } else {
            profile.setId(stale.getId());
        }
        dao.save(profile);
    }

    @Autowired
    public void setDao(StoreProfileDao dao) {
        this.dao = dao;
    }
}
